package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.MessageManager;
import com.itheima.app.manager.UserManager;
import com.itheima.vo.HuanXinVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.GET;
import java.util.Map;

@RestController
public class MessageController {

    //获取环信账号密码
    @GetMapping("/huanxin/user")
    public ResponseEntity findHuanxinUser() {
        Long userId = UserHolder.get().getId();
        HuanXinVo vo = new HuanXinVo();
        vo.setUsername("HX" + userId);
        vo.setPassword("123456");
        return ResponseEntity.ok(vo);
    }

    @Autowired
    private MessageManager messageManager;

    //打招呼
    @PostMapping("/tanhua/strangerQuestions")
    public void strangerQuestions(@RequestBody Map<String, String> param) {
        Long jiarenId = Long.parseLong(param.get("userId"));
        String reply = param.get("reply");
        messageManager.strangerQuestions(jiarenId, reply);
    }

    //添加好友
    @PostMapping("/messages/contacts")
    public void addContacts(@RequestBody Map<String, Long> param) {
        Long friendId = param.get("userId");

        messageManager.addContacts(friendId);
    }

    //联系人列表
    @GetMapping("/messages/contacts")
    public ResponseEntity findContactVoByPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                              @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return messageManager.findContactVoByPage(pageNum, pageSize);

    }

    @Autowired
    private UserManager userManager;

    // 根据huanxinId查询用户基本信息
    @GetMapping("/messages/userinfo")
    public ResponseEntity huanxinUserInfo(String huanxinId) {
        String hx = huanxinId.replace("HX", "");

        return userManager.findUserInfoVo(Long.valueOf(hx));
    }

    // 查看点赞列表
    @GetMapping("/messages/likes")
    public ResponseEntity findLikes(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        Integer commentType = 1;
        return messageManager.findUserCommentVoByPage(commentType, pageNum, pageSize);
    }

    // 查看评论列表
    @GetMapping("/messages/comments")
    public ResponseEntity findComments(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        Integer commentType = 2;
        return messageManager.findUserCommentVoByPage(commentType, pageNum, pageSize);
    }

    //查看喜欢列表
    @GetMapping("/messages/loves")
    public ResponseEntity findLoves(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        Integer commentType = 3;
        return messageManager.findUserCommentVoByPage(commentType, pageNum, pageSize);
    }

    //公告
    @GetMapping("/messages/announcements")
    public ResponseEntity announcementsByPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                              @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return messageManager.announcementsByPage(pageNum, pageSize);
    }
}
